<?php
/**
 * file exclusivevideocontroller.php
 * Created on 2015-4-17 下午5:59:51 
 * Create by xiweijie
 */
namespace haibao\cms\business\control\pc\channel\exclusive;

use haibao\frame\data\query\Condition;
use haibao\cms\model\filter\Video as VideoFilter;
use haibao\cms\model\filter\ImageLibrary as ImageLibraryFilter;

class ExclusiveVideoController extends \haibao\cms\business\control\pc\channel\exclusive\ExclusiveController{

    public function __construct($updateTime = null,$controlId,$isPublish){
        parent::__construct($updateTime,$controlId,$isPublish);
    }

    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        
        $this->getVideo();
        $this->assign('exclusiveHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE));
        $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        $content = $this->replaceCDNLink($content);
        return $content;
    }

    /**
     * 获取一条有封面的视频
     */
    private function getVideo(){
        
    	$videoFilter = new VideoFilter();
    	$videoFileFilter = new \haibao\cms\model\filter\VideoFile();
    	$videoCategoryFilter = new \haibao\cms\model\filter\VideoCategory();
    	
    	$videoFilter->select(array(
    			VideoFilter::CONDITION_FIELD_NAME_ID,
    			VideoFilter::CONDITION_FIELD_NAME_TITLE,
    			VideoFilter::CONDITION_FIELD_NAME_VIDEOURL,
    			VideoFilter::CONDITION_FIELD_NAME_URLH5,
    			VideoFilter::CONDITION_FIELD_NAME_LASTMODIFYUSER,
    			VideoFilter::CONDITION_FIELD_NAME_LASTMODIFYTIME,
    			VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,
    			VideoFilter::CONDITION_FIELD_NAME_CATEGORYID,
    			VideoFilter::CONDITION_FIELD_NAME_PUBLISHED
    	));
    	$videoFilter->order(VideoFilter::CONDITION_FIELD_NAME_ID,false);
    	$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::STATUS_ENABLE);
    	$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_PUBLISHED, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::PUBLISHED_OK);
    	$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_SURFACE_PICID, Condition::CONDITION_IS_NOT_NULL,null);
    	
    	$imageLibraryFilter = new ImageLibraryFilter();
    	$imageLibraryFilter->select(array(
    			ImageLibraryFilter::CONDITION_FIELD_NAME_FILENAME,
    	));
    	$videoFilter->innerJoin($imageLibraryFilter,$videoFilter,VideoFilter::CONDITION_FIELD_NAME_SURFACE_PICID,ImageLibraryFilter::CONDITION_FIELD_NAME_ID,VideoFilter::CONDITION_FIELD_NAME_PIC_URL,false);
    	
    	$videoFileFilter->select(array(
    	    \haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_ID,
    	    \haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_VIDEOID,
    	    \haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_STATUS,
    	    \haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_FILEPATH
    	));
    	
    	$videoFilter->leftJoin($videoFileFilter,null,VideoFilter::CONDITION_FIELD_NAME_ID,\haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_VIDEOID,VideoFilter::CONDITION_FIELD_NAME_VIDEOFILE,false);
    	
    	$videoCategoryFilter->select(array(
    	    \haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_ID,
    	    \haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_CATEGORYNAME
    	));
    	$videoFilter->leftJoin($videoCategoryFilter,null,VideoFilter::CONDITION_FIELD_NAME_CATEGORYID,\haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_ID,VideoFilter::CONDITION_FIELD_NAME_CATEGORY,false);
    	
    	$data = new \haibao\cms\data\Video();
    	$videoInfo = $data->getOne($videoFilter);
    	if($videoInfo){
    		$videoInfo->PublishDate = $this->formatDate($videoInfo->PublishDate->format('Y-m-d H:i:s'));
    		$exclusiveObj = ExclusiveController::getExclusiveInstance();
    		$exclusiveObj->videoId = $videoInfo->Id;
    		$videoInfo->VideoUrl = preg_replace('/autoplay=(\d+)/', 'autoplay=0', $videoInfo->VideoUrl);
    	}
    	
    	$this->assign('videoInfo',$videoInfo);
    	
    }
    
}
